package com.jsits.v2x.common.util;

import org.apache.commons.lang3.ArrayUtils;
import  org.apache.commons.lang3.StringUtils;

public class ArrayUtil
{

	public static String toString(String... msgs)
	{
		if (msgs == null)
		{
			return "";
		}
		StringBuffer sb = new StringBuffer();
		for (String msg : msgs)
		{
			sb.append(msg);
		}
		return sb.toString();

	}

	public static String[] tostrs(String str)
	{
		if ( StringUtils.isBlank(str))
		{
			return null;
		}
		if (str.charAt(0) == '[' && str.charAt(str.length()-1) == ']')
		{
			str = str.substring(1, str.length() - 1);
		}

		return StringUtils.split(str, ",");

	}

	public static int[] toInts(String... strs)
	{
		if (ArrayUtils.isEmpty(strs))
		{
			return null;
		}

		int[] values = new int[strs.length];

		for (int i = 0; i < strs.length; i++)
		{
			int value = 0;
			try
			{
				value = Integer.parseInt(strs[i]);
			} catch (Exception e)
			{
			}
			values[i] = value;
		}
		return values;
	}

	public static int[] toInts(String str)
	{
		if (StringUtils.isBlank(str))
		{
			return null;
		}
		if (str.charAt(0) == '[' && str.charAt(str.length()-1) == ']')
		{
			str = str.substring(1, str.length() - 1);
		}

		String[] values = StringUtils.split(str, ",");

		return toInts(values);

	}

	public static boolean[] toBools(String... strs)
	{
		if (ArrayUtils.isEmpty(strs))
		{
			return null;
		}

		boolean[] values = new boolean[strs.length];

		for (int i = 0; i < strs.length; i++)
		{
			boolean value = false;
			try
			{
				value = Boolean.parseBoolean(strs[i]);
			} catch (Exception e)
			{
			}
			values[i] = value;
		}
		return values;
	}

	public static float[] toFloats(String... strs)
	{
		if (ArrayUtils.isEmpty(strs))
		{
			return null;
		}

		float[] values = new float[strs.length];

		for (int i = 0; i < strs.length; i++)
		{
			float value = 0;
			try
			{
				value = Float.parseFloat(strs[i]);
			} catch (Exception e)
			{
			}
			values[i] = value;
		}
		return values;
	}

	public static float[] toFloats(String str)
	{
		if (StringUtils.isBlank(str))
		{
			return null;
		}
		if (str.charAt(0) == '[' && str.charAt(str.length()-1) == ']')
		{
			str = str.substring(1, str.length() - 1);
		}

		String[] values = StringUtils.split(str, ",");

		return toFloats(values);

	}

	public static boolean[] toBooleans(String str)
	{
		if (StringUtils.isBlank(str))
		{
			return null;
		}
		if (str.charAt(0) == '[' && str.charAt(str.length()-1) == ']')
		{
			str = str.substring(1, str.length() - 1);
		}

		String[] values = StringUtils.split(str, ",");

		return toBools(values);

	}
	
	public static double[] toDoubles(String str)
	{
		if (StringUtils.isBlank(str))
		{
			return null;
		}
		if (str.charAt(0) == '[' && str.charAt(str.length() - 1) == ']')
		{
			str = str.substring(1, str.length() - 1);
		}

		String[] values = StringUtils.split(str, ",");

		return toDoubles(values);

	}
	public static double[] toDoubles(String... strs)
	{
		if (ArrayUtils.isEmpty(strs))
		{
			return null;
		}

		double[] values = new double[strs.length];

		for (int i = 0; i < strs.length; i++)
		{
			double value = 0;
			try
			{
				value = Float.parseFloat(strs[i]);
			} catch (Exception e)
			{
			}
			values[i] = value;
		}
		return values;
	}

}
